package cn.mesmile.nacos.model;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.mesmile.nacos.dto.Datas;
import cn.mesmile.nacos.dto.Series;
import cn.mesmile.nacos.dto.StatisticalResults;
import cn.mesmile.nacos.dto.XAxis;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2021/6/18 23:40
 * @Description: 本周数据
 */
public class ThisWeekData extends Strategy{

    @Override
    StatisticalResults getStatisticsResult(List<Datas> dataList) {
        // todo 获取所有老用户id 放在此变量中
        Set<String> oldUserId = new HashSet<>();
        oldUserId.add("123456");


        // 1表示周日，2表示周一,3表示周二
        int whichDay = DateUtil.thisDayOfWeek();
        if (whichDay == 1) {
            // 这里为了方便把周日做特殊处理
            whichDay = 8;
        }
        List<String> dataX = new ArrayList<>();

        List<Integer> dataYScanningY = new ArrayList<>(whichDay - 1);
        List<Integer> oldUserY = new ArrayList<>(whichDay -1);
        List<Integer> newUserY = new ArrayList<>(whichDay - 1);
        List<Integer> numberOfPeopleY = new ArrayList<>(whichDay -1);
        // 组装 x 轴数据 和 初始化 y 轴的值
        for (int i = 1; i <= whichDay -1; i++) {
            dataX.add(getWeekChinese(i));
            dataYScanningY.add(0);
            oldUserY.add(0);
            newUserY.add(0);
            numberOfPeopleY.add(0);
        }
        XAxis xAxis = new XAxis();
        xAxis.setData(dataX);

        List<Series> seriesList = new ArrayList<>(4);
        ////////////////扫码次数///////////////////
        Series scanningTimes = new Series();
        scanningTimes.setName("扫码次数");
        ////////////////老用户数///////////////////
        Series oldUser = new Series();
        oldUser.setName("老用户数");
        ////////////////新用户数///////////////////
        Series newUser = new Series();
        newUser.setName("新用户数");
        ////////////////老用户数///////////////////
        Series numberOfPeople = new Series();
        numberOfPeople.setName("曝光总人数");


        // key 1表示周日，2表示周一,3表示周二 ， value 为次数
        Map<Integer, List<Datas>> data = dataList.stream().collect(Collectors.groupingBy(series ->
                // 1表示周日，2表示周一,3表示周二
                DateUtil.dayOfWeek(series.getCreateDate()))
        );

        Iterator<Map.Entry<Integer, List<Datas>>> iterator = data.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, List<Datas>> next = iterator.next();
            // x轴下标
            Integer key = next.getKey();
            // 次数
            List<Datas> value = next.getValue();
            // 时段的总次数
            int totalSize = value.size();
            // 老用户扫码次数
            long oldUserCount = value.stream().filter(datas -> oldUserId.contains(datas.getSharedCustomerId())).count();
            // 根据扫码人id去重
            long count = value.stream().map(Datas::getSharedCustomerId).distinct().count();

            if (key == 1){
                // 对周日做特殊处理
                key = 8;
            }
            // 对齐下标
            key = key - 2;

            dataYScanningY.set(key, totalSize);
            oldUserY.set(key, Convert.toInt(oldUserCount));
            newUserY.set(key, Convert.toInt(totalSize - oldUserCount));
            numberOfPeopleY.set(key, Convert.toInt(count));
        }
        scanningTimes.setData(dataYScanningY);
        oldUser.setData(oldUserY);
        newUser.setData(newUserY);
        numberOfPeople.setData(numberOfPeopleY);

        seriesList.add(scanningTimes);
        seriesList.add(oldUser);
        seriesList.add(newUser);
        seriesList.add(numberOfPeople);

        return new StatisticalResults(seriesList, xAxis);
    }


    private String getWeekChinese(int dayOfWeek){
        if (dayOfWeek == 1){
            return "周一";
        } else if (dayOfWeek == 2){
            return "周二";
        }else if (dayOfWeek == 3){
            return "周三";
        }else if (dayOfWeek == 4){
            return "周四";
        }else if (dayOfWeek == 5){
            return "周五";
        }else if (dayOfWeek == 6){
            return "周六";
        }else {
            return "周日";
        }
    }

}
